/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","comm/datatable/datatable","comm/form/form",    
    "bch/app/visit/visitAppointList.ui",
    "bch/app/visit/visitAppointAdd.servers"],
    function($,$bb,$datatable,$form,$ui,$visitAppointAddServers){
	var servers = {};
	var URL = $bb.baseUrl("followapp");

    /**
     * 请求访视当天提醒
     */
    servers.ajaxRemindPatientRecord = function(callback){
        $bb.ajax({
            url: URL + "ctrl/patientRecord/remindPatientRecord",
            success: function(data){
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询访视当天提醒出错！");
                }   
            }
        });
    }

    /**
     * 访视状态改变后的回调函数
     */
    var ajaxSubmitPatientRecordCallBack = function(data){
        $datatable.table.api().draw(false);
        var args = $datatable.table.api().ajax.params();   //获取dt请求参数
    }

    /**
     * 修改访视状态
     */
    var ajaxModifyStatus = function(params,callback){
        $bb.ajax({
            url: URL + "ctrl/patientRecord/modifyPatientRecordStatus",
            data:JSON.stringify({
                data : params
            }),
            success: function(data){
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "修改访视状态出错！");
                }   
            }
        });
    }

    /**
     * 提交“确认已访视”
     */
    var onSubmit = function(){
        var params = $(this).data("params");
        $("." + params.className).modal("hide");
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var channelId = $bb.getChannelId();
        var userName = $bb.code.getValueByName("visitConfirm","userName");
        var contList = JSON.parse(params.info.templData);
        var arg = {
            "oprInfo":{
                "opCode":"1214",
                "opType":"2",
                "opNote":"【"+ doctorName +"】医生给【"+ userName +"】患者添加访视预约信息",
                "channelType":"2",
                "channelId":channelId,
            },
            "visitInfo":{
                "contList":contList,
                "id":params.info.id,
                "flag":"3"
            }
        }
        ajaxModifyStatus(arg,ajaxSubmitPatientRecordCallBack);
    }
    /**
     * 请求答案成功后的回调函数
     */
    var ajaxPatientRecordCallBack = function(data,questions,params){
        $form.init({
            id: "visitConfirm",
            status: "readonly",
            questions: questions,
            values: data.valueList,
        });
        $("." + params.className).modal("show");
        $("#submit").unbind("click");
        $("#submit").data("params",params);
        $("#submit").click(onSubmit);
    }
    /**
     * 请求访视预约问卷成功后的回调函数
     */
    var ajaxQuestionCallBack = function(data,params){
        $visitAppointAddServers.ajaxPatientRecord(ajaxPatientRecordCallBack,data.rows,params);
    }
    /**
     * 点击“未访视”按钮弹出访视确认框
     */
    var onVisitConfirm = function(){
        var params = $(this).data("params");
        var className = "visitConfirmModal";
        $bb.modal({
            "className": className,
            "tit": "访视确认",
        },function(){
            $ui.visitConfirmWrap({
                "dom":$("." + className + " .modal-body")
            });
            params.className = className;
            $visitAppointAddServers.getQuestiones(params,ajaxQuestionCallBack);
        });
    }

    /**
     * 跳转到新增、修改、查看页面
     */
    var onGovisitAppointAdd = function(){
        var params = $(this).data("params");
        $bb.openPage("bch_visitAppointAdd",params);
    }

    /******************** datatable ********************/
    
    /**
     * 入参
     */
    var inParams = function(args){
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "userName": $.trim($('#userName').val()),
                "phoneNo": $.trim($('#phoneNo').val()),
                "address": $.trim($('#address').val()),
                "visitType": $.trim($('#visitType').val()),
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsVisitType = function(e){
        return e.visitType == "1" ? "首次访视" : "满月访视";
    }
    var columnsData = function(){
        var columnsArr = [
            {"data":columnsVisitType},
            {"data":'userName'},
            {"data":'phoneNo'},
            {"data":'address'},
            {"data":'doorplate'},
            {"data":"contactDate"},
            {"data":'appointmentTime'},
            {
                "data":null,
                "className":"operate"
            }
        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        var h = data.status == 2 ? '<div class="btn-solid btn-solid-gray cursor-disabled">已访视</div><div class="btn-solid btn-solid-gray look">查看</div>' : '<div class="btn-solid btn-solid-blue no-visit">未访视</div><div class="btn-solid btn-solid-green edit">修改</div>';
        $('td.operate', row).html(h);
        $("td.operate .no-visit",row).unbind("click");
        $("td.operate .no-visit",row).data("params",{"info":data});
        $("td.operate .no-visit",row).click(onVisitConfirm);
        $("td.operate .look,td.operate .edit,td.operate .appoint",row).unbind("click");
        $("td.operate .look",row).data("params",{"info":data,"status":"01"}); // 01:查看
        $("td.operate .edit",row).data("params",{"info":data,"status":"02"}); // 02:修改
        $("td.operate .appoint",row).data("params",{"info":data,"status":"03"}); // 03:预约
        $("td.operate .look,td.operate .edit,td.operate .appoint",row).click(onGovisitAppointAdd);
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        var id = $(".layout-bottom table").attr("id");
        $("#" + id).prev("div.row").remove();
        $("#serchBtn").bind("click",function(){
            $datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatable.table.api().ajax.params();   //获取dt请求参数
        });
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "ctrl/patientRecord/qryPatientRecord?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

	return servers;
});